// **********************************************************************
//                            Project Nitro
//                        ---------------------
//
//  File        : Application.cpp
//  Description : Application main class
//  Author      : Guillaume Delhumeau <guillaume.delhumeau@gmail.com>
//  License     : GNU General Public License 3
//
// **********************************************************************

#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>

#include "Application.h"
#include "GameEngine.h"
#include "Preferences.h"
#include "Maths/unit_tests.h"
#include "Memory.h"

/************************************************************************/
/* Main function                                                        */
/************************************************************************/
int Application::main(const std::vector<CL_String> &args)
{	
	init();
	bool run_game = check_args(args);
	if(run_game)
	{
		try
		{
			GameEngine game;
			game.init();
			game.run();
			game.term();
		}
		// Catch any errors from ClanLib
		catch(CL_Exception & exception)
		{
			CL_ConsoleWindow console("Console", 80, 160);
			CL_Console::write_line("Exception caught:");
			CL_Console::write_line(exception.message);

			CL_Console::write_line("--------------------");

			// Display the stack trace (if available)
			std::vector<CL_String> stacktrace = exception.get_stack_trace();
			size_t size = stacktrace.size();
			if (size > 0)
			{
				CL_Console::write_line("Stack Trace:");
				for (unsigned int cnt=0; cnt < size; cnt++)
				{
					CL_Console::write_line(stacktrace[cnt]);
				}
			}
			console.display_close_message();
			return -1;
		}

	}
	term();
	
	return 0;
}

/************************************************************************/
/* Init application                                                     */
/************************************************************************/
void Application::init()
{
#ifdef DEBUG
	maths_unit_tests();
#endif
	init_memory();
// 	common_resources_init();
// 	g_audio_manager.init();		
// 	Preferences *pref = pref_get_instance();
}

/************************************************************************/
/* Terminate application                                                */
/************************************************************************/
void Application::term()
{
// 	common_resources_term();		
// 	g_audio_manager.term();
// 	my_delete(pref_get_instance());
 	term_memory();
}



/************************************************************************/
/* Checking command lines arguments                                     */
/************************************************************************/
bool Application::check_args(const std::vector<CL_String> &args)
{
	bool run_game = true;
	//Preferences *pref = pref_get_instance();

	// Checking parameters
	for(unsigned int i = 0; i < args.size(); ++i)
	{
		// Render target
		if((args[i] =="--gdi") && g_preferences.render_target != GDI)
		{
			g_preferences.render_target = GDI;
			g_preferences.write();
		}
		else if((args[i] == "--opengl") && g_preferences.render_target != OPENGL_1)
		{
			g_preferences.render_target = OPENGL_1;
			g_preferences.write();
		}
		else if((args[i] == "--opengl2") && g_preferences.render_target != OPENGL_2)
		{
			g_preferences.render_target = OPENGL_2;
			g_preferences.write();
		}
		else if((args[i] == "--dx9") && g_preferences.render_target != DIRECTX_9)
		{
			g_preferences.render_target = DIRECTX_9;
			g_preferences.write();
		}
		else if((args[i] == "--dx10") && g_preferences.render_target != DIRECTX_10)
		{
			g_preferences.render_target = DIRECTX_10;
			g_preferences.write();
		}
		// Help
		else if((args[i] == "--help"))
		{
			print_help();
			run_game = false;
		}
		// License
		else if((args[i] == "--license"))
		{
			print_license();
			run_game = false;
		}
	}

	return run_game;
}


/************************************************************************/
/* Help function                                                        */
/************************************************************************/
void Application::print_help()
{
	std::cout << "Project Nitro " << " -- Help" << std::endl
		<< "Usage: nitro [OPTIONS]"  << std::endl  << std::endl
		<< "Options:"  << std::endl
		<< "\t--help     : Show this message"  << std::endl
		<< "\t--license  : Show the license of this program"  << std::endl		
		<< "\t--gdi      : Use GDI as render target (default)" << std::endl
		<< "\t--opengl   : Use OpenGL as render target" << std::endl
		<< "\t--opengl2  : Use OpenGL 2 as render target" << std::endl
		<< "\t--sdl      : Use SDL as render target" << std::endl;		
}

/************************************************************************/
/* Display the license                                                  */
/************************************************************************/
void Application::print_license()
{
	std::cout << " * Project Nitro -- License\n" << std::endl
		<< " * Copyright (C) 2009 Guillaume Delhumeau <guillaume.delhumeau at gmail.com>" << std::endl
		<< " *"  << std::endl
		<< " * This program is free software; you can redistribute it and/or modify " << std::endl
		<< " * it under the terms of the GNU General Public License as published by" << std::endl
		<< " * the Free Software Foundation; either version 3 of the License, or" << std::endl
		<< " * (at your option) any later version." << std::endl
		<< " *" << std::endl
		<< " * This program is distributed in the hope that it will be useful," << std::endl
		<< " * but WITHOUT ANY WARRANTY; without even the implied warranty of" << std::endl
		<< " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" << std::endl
		<< " * GNU General Public License for more details." << std::endl
		<< " *" << std::endl
		<< " * You should have received a copy of the GNU General Public License" << std::endl
		<< " * along with this program; if not, write to the Free Software" << std::endl
		<< " * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." <<std::endl
		<< " *" << std::endl << std::endl;
}
